/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 *
 * Author: zhaoshujin
 * Date: 2011-6-2 上午11:12:45
 */
package com.md5;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author zhaoshujin
 *
 */
public class MD5 {
	public synchronized static String encrypt(String src) {
		src = src.trim();

		String Digest = "";
		try {
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			md5.reset();
			byte[] mess = null;
			try {
				mess = src.getBytes("UTF-8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			byte[] hash = md5.digest(mess);
			for (int i = 0; i < hash.length; i++) {
				int v = hash[i];
				if (v < 0)
					v = 256 + v;
				if (v < 16)
					Digest += "0";
				Digest += Integer.toString(v, 16).toUpperCase() + "";
			}
		} catch (NoSuchAlgorithmException e) {
			System.out.println("MD5加密过程中失败！");
			e.printStackTrace();
		}
		return Digest;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String md5 = encrypt("888888");
		System.out.println(md5);
	}

}